% little script that will take you through selecting all the stuff you need
% to manually select in order to then automatically run the high res
% tracking script


%% make sure these parameters are appropriate!!
isReferenceSession = false;
localimagesize = [1800,1800];        %in pixels; important for loading the .raw files; should be in [width,height]
referenceimagesize = [1832,1736];
micronsPerPixel = (1/.449);
rootname = 'opto';

%% will be prompted to select stuff:
if isReferenceSession
    localimagesize = referenceimagesize;
    [fn,fd] = uigetfile('*.raw','select one .raw file in the experimental sequence');
    cd(fd);
    movdirectory = fd;
    mov = loadRawStack_v1(fn(1:10),referenceimagesize,1:100);
    avios = mov(:,:,2:2:end);
    avios = median(avios,3);
    disp('select vessel centers')
    % select vessel centers:
    [~,~,vesselMasks] = selectVesselCenters(avios,[],'gray');
    % select window mask for reference image
    disp('select window mask')
    figure;imagesc(avios);colormap('gray');axis equal
    roi = drawpolygon;refim_windowmask = createMask(roi);
    windowmask = refim_windowmask;
    selpath = uigetdir(cd,'Select directory where to save reference image data');
    cd(selpath);
    save('vesselmaskdata.mat','refim_windowmask','avios','vesselMasks','referenceimagesize','micronsPerPixel')
    vesselmaskname = [cd,'\vesselmaskdata.mat'];
else
    [fn,fd] = uigetfile('*.mat','select reference session data');
    vesselmaskname = [fd,fn];
    [fn,fd] = uigetfile('*.raw','select one .raw file in the sequence');
    cd(fd);
    mov = loadRawStack_v1(fn(1:10),localimagesize,1:100);
    movdirectory = fd;
    avios = mov(:,:,2:2:end);
    avios = median(avios,3);
    figure;imagesc(avios);colormap('gray');axis equal
    disp('select window mask')
    roi = drawpolygon;windowmask = createMask(roi);
end
bgdirectory = uigetdir(cd,'select background image directory');
[fn,fd] = uigetfile('*.mat','select metadata file');
metaname = [fd,fn];
savedirectory = fd;
[fn,fd] = uigetfile('*.mat','select background metadata file');
bgmetaname = [fd,fn];

savename = input('save results as: ',"s");

cd(savedirectory);
save('referencingInfo.mat','vesselmaskname','movdirectory','windowmask','bgdirectory','metaname',...
    'bgmetaname','savedirectory','savename','rootname','localimagesize',...
    'isReferenceSession');


